package com.watson.nettydemo.demo02;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.UUID;

/**
 * // todo
 *
 * @author: llun
 * @date: 2022/3/11 14:54
 * @version: 1.0
 */
public class SocketServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        System.out.println(ctx.channel().remoteAddress() + "" + msg);
        Thread.sleep(1000);
        ctx.channel().writeAndFlush("from Server:" + UUID.randomUUID().toString());
    }

    /**
     * 当channel被激活以后会触发该方法的执行。互发消息的第一条
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ctx.channel().writeAndFlush("from Server: begin" );
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}
